package org.jeecg.modules.htxincailiao.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.order.entity.HTOrderReport;
import org.jeecg.modules.htxincailiao.order.service.IHTOrderReportService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: ht_order_report
 * @Author: jeecg-boot
 * @Date: 2021-11-30 16:34:16
 * @Version: V1.0
 */
@Api(tags = "ht_order_report")
@RestController
@RequestMapping("/order/hTOrderReport")
@Slf4j
public class HTOrderReportController extends JeecgController<HTOrderReport, IHTOrderReportService> {
    @Resource
    private IHTOrderReportService hTOrderReportService;

    @Resource
    private IHTAccountService accountService;

    /**
     * 分页列表查询
     *
     * @param hTOrderReport
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_order_report-分页列表查询")
    @ApiOperation(value = "ht_order_report-分页列表查询", notes = "ht_order_report-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HTOrderReport hTOrderReport,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HTOrderReport> queryWrapper = QueryGenerator.initQueryWrapper(hTOrderReport, req.getParameterMap());
        Page<HTOrderReport> page = new Page<HTOrderReport>(pageNo, pageSize);
        IPage<HTOrderReport> pageList = hTOrderReportService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTOrderReport
     * @return
     */
    @AutoLog(value = "ht_order_report-添加")
    @ApiOperation(value = "ht_order_report-添加", notes = "ht_order_report-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTOrderReport hTOrderReport) {
        hTOrderReportService.save(hTOrderReport);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTOrderReport
     * @return
     */
    @AutoLog(value = "ht_order_report-编辑")
    @ApiOperation(value = "ht_order_report-编辑", notes = "ht_order_report-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTOrderReport hTOrderReport) {
        hTOrderReportService.updateById(hTOrderReport);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_order_report-通过id删除")
    @ApiOperation(value = "ht_order_report-通过id删除", notes = "ht_order_report-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTOrderReportService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_order_report-批量删除")
    @ApiOperation(value = "ht_order_report-批量删除", notes = "ht_order_report-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTOrderReportService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_order_report-通过id查询")
    @ApiOperation(value = "ht_order_report-通过id查询", notes = "ht_order_report-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HTOrderReport hTOrderReport = hTOrderReportService.getById(id);
        if (hTOrderReport == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTOrderReport);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_order_report-通过id查询")
    @ApiOperation(value = "ht_order_report-通过id查询", notes = "ht_order_report-通过id查询")
    @RequestMapping(value = "/getUserOrderReportInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> getUserOrderReportInfo(HttpServletRequest req, @RequestParam(name = "id", required = false) String id) {
        String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
        if (oConvertUtils.isEmpty(token)) {
            return Result.error("请选登录！");
        }

		Result<JSONObject> result = new Result<JSONObject>();
        JSONObject resultObj = new JSONObject();
        String username = JwtUtil.getUsername(token);
        HTPlatformUser user = accountService.queryFrontendUserByUsername(username);
        if (oConvertUtils.isNotEmpty(user)) {
            if (req.getMethod().equals("POST")) {
                String line = RequestUtil.getPostParam(req);
                if (oConvertUtils.isNotEmpty(line)) {
                    HTOrderReport hTOrderReport = JSONObject.parseObject(line, HTOrderReport.class);
                    id = hTOrderReport.getId();
                }
            }

            HTOrderReport hTOrderReport = hTOrderReportService.findBy(user.getId(), id);
            if (oConvertUtils.isNotEmpty(hTOrderReport)) {
                resultObj.put("ordered", true);
            } else {
                resultObj.put("ordered", false);
            }

			result.setResult(resultObj);

            return result;
        } else {
            return Result.error("用户不存在！");
        }
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTOrderReport
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTOrderReport hTOrderReport) {
        return super.exportXls(request, hTOrderReport, HTOrderReport.class, "ht_order_report");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTOrderReport.class);
    }

}
